package com.iqiyi.pps.epg.api.server.proxy;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.aop.annotation.SkipAuthCheck;
import com.iqiyi.pps.epg.api.aop.annotation.SkipInterfaceLog;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.query.TResponse;
import com.iqiyi.pps.epg.api.model.web.vod.*;
import com.iqiyi.pps.epg.api.server.web.vod.VodWebService;
import com.iqiyi.pps.epg.api.utils.Constants;
import com.iqiyi.pps.epg.api.utils.RegUtil;
import com.iqiyi.pps.epg.api.utils.ServiceUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2017/7/31.
 */
@Service
public class VodProxy extends BaseProxy {
    private static Logger logger = LoggerFactory.getLogger(OtherProxy.class);

    @SkipInterfaceLog
    public String getVodLogList(String channelId, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            Map<String, String> params = new HashMap<>();
            TFilter tFilter = new TFilter();
            params.put("EQI_channelId", channelId + "");
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            TVodLogPage tVodLogPage = service.searchLog(page, tFilter);
            if (tVodLogPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tVodLogPage, "操作成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getVodLogList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String getVodLogListByFilter(String paramsObj , Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            Map<String, String> params = new HashMap<>();
            TFilter tFilter = new TFilter();
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            TVodLogPage tVodLogPage = service.searchLog(page, tFilter);
            if (tVodLogPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tVodLogPage, "操作成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getVodLogList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String getVodEpisodeLogList(String paramsObj, Integer pageNo, Integer pageSize){
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            Map<String, String> params = new HashMap<>();
            TFilter tFilter = new TFilter();
            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            TVodEpisodeLogPage tVodLogPage = service.searchEpisodeLog(page, tFilter);
            if (tVodLogPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tVodLogPage, "操作成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getVodLogList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    @SkipAuthCheck
    public String saveEpisodeLog(String dataString){
        try {
            TVodEpisodeLog entity = JSONObject.parseObject(dataString, TVodEpisodeLog.class);
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            if (entity == null || entity.getProgramId() < 1) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }
            long ret = service.saveEpisodeLog(entity);
            if (ret > 1) {
                return Constants.getResponse(Constants.CODE_SUC, ret, "操作成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, ret, INTERFACE_ERROR);
            }
        } catch (Exception e) {
            logger.error("[saveVodEpisodeLog]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    public String saveVodLog(String dataString) {
        try {
            TVodLog entity = JSONObject.parseObject(dataString, TVodLog.class);
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            if (entity == null || entity.getChannelId() < 1) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }
            entity.setUser(ServiceUtil.getOpUser());
            if (entity.getUser() == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, "操作人员异常");
            }
            long ret = service.saveLog(entity);
            if (ret > 1) {
                return Constants.getResponse(Constants.CODE_SUC, ret, "操作成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, ret, INTERFACE_ERROR);
            }
        } catch (Exception e) {
            logger.error("[saveVodLog]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    @SkipInterfaceLog
    public String getVodRecordList(String channelId, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            Map<String, String> params = new HashMap<>();
            TFilter tFilter = new TFilter();
            params.put("EQI_channelId", channelId + "");
            params.put("EQI_status", "1");
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            TVodRecordPage tVodRecordPage = service.searchRecord(page, tFilter);
            if (tVodRecordPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tVodRecordPage, "操作成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getVodRecordList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    public String addVodRecord(String dataString) {
        try {
            TVodRecord entity = JSONObject.parseObject(dataString, TVodRecord.class);
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            if (entity == null || entity.getChannelId() < 1) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }
            if (!RegUtil.isDateFormat(entity.getStartTime())) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, "设定收录时间异常");
            }
            if (entity.getEndTime() != null) {
                if (!RegUtil.isDateFormat(entity.getEndTime()) || entity.getEndTime().compareTo(entity.getStartTime()) <= 0) {
                    return Constants.getJsonResponse(Constants.CODE_FAIL, null, "设定收录结束时间异常");
                }
            }

            entity.setOpUser(ServiceUtil.getOpUser());
            if (entity.getOpUser() == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, "操作人员异常");
            }
            TResponse response = service.addRecord(entity);
            String descr = null;
            if (response == null) {
                descr = "null|" + getStrParam();
            } else {
                descr = response.getCode() + "|" + response.getJsonData() + "|" + getStrParam();
            }
            writeVodLog(entity.getChannelId(), Constants.VodLogType.VOD_RECORD_TASK_ADD.getIndex(), null, descr);
            if (response == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else if (Constants.CODE_SUC.equals(response.getCode())) {
                return Constants.getResponse(Constants.CODE_SUC, null, "保存成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, response.getMsg());
            }

        } catch (Exception e) {
            logger.error("[addVodRecord]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    public String editVodRecord(String dataString) {
        try {
            TVodRecord entity = JSONObject.parseObject(dataString, TVodRecord.class);
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            if (entity == null || entity.getId() < 1) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }
            if (!RegUtil.isDateFormat(entity.getStartTime())) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, "设定收录时间异常");
            }
            if (entity.getEndTime() != null) {
                if (!RegUtil.isDateFormat(entity.getEndTime()) || entity.getEndTime().compareTo(entity.getStartTime()) <= 0) {
                    return Constants.getJsonResponse(Constants.CODE_FAIL, null, "设定收录结束时间异常");
                }
            }
            TResponse response = service.editRecord(entity);
            writeVodLog(entity.getChannelId(), Constants.VodLogType.VOD_RECORD_TASK_EDIT.getIndex(), null, (response == null ? "null" : response.getCode() + "|[" + response.getMsg() + "]") + "|" + getStrParam());
            if (response == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else if (Constants.CODE_SUC.equals(response.getCode())) {
                return Constants.getResponse(Constants.CODE_SUC, null, "修改成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, response.getMsg());
            }
        } catch (Exception e) {
            logger.error("[editVodRecord]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    public String delVodRecord(String channelId, String taskId) {
        try {
            TFilter tFilter = new TFilter();
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            Map<String, String> params = new HashMap<>();
            if (!RegUtil.isPositiveInteger(channelId)) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }
            params.put("EQI_channelId", channelId + "");
            if (taskId != null && RegUtil.isPositiveInteger(taskId)) {
                params.put("EQL_id", taskId + "");
            }
            tFilter.setFilters(params);
            TResponse response = service.delRecord(tFilter);
            writeVodLog(Integer.valueOf(channelId), Constants.VodLogType.VOD_RECORD_TASK_DEL.getIndex(), null, (response == null ? "null" : response.getCode()) + getStrParam());
            if (response == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else if (Constants.CODE_SUC.equals(response.getCode())) {
                return Constants.getResponse(Constants.CODE_SUC, null, "删除成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, "删除失败");
            }
        } catch (Exception e) {
            logger.error("[delVodRecord]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    public String addVodad (String dataString) {
        try {
            TVodad entity = JSONObject.parseObject(dataString, TVodad.class);
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            if (entity == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }

            //entity.setOpUser(ServiceUtil.getOpUser());
            TResponse response = service.saveAd(entity);
            if (response == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else if (Constants.CODE_SUC.equals(response.getCode())) {
                return Constants.getResponse(Constants.CODE_SUC, null, "保存成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, response.getMsg());
            }

        } catch (Exception e) {
            logger.error("[addVodad]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }

    @SkipAuthCheck
    public String getVodadList (String paramsObj, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        try {
            pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
            pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            Map<String, String> params = new HashMap<>();

            if (paramsObj != null) {
                JSONObject jsonObject = JSONObject.parseObject(paramsObj);
                for (Map.Entry<String, Object> temp : jsonObject.entrySet()) {
                    params.put(temp.getKey(), temp.getValue() + "");
                }
            }
            params.put("EQI_status", "0");
            TFilter tFilter = new TFilter();
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "addTime", "desc", true);
            TVodadPage tVodadPage = service.searchAd(page, tFilter);
            if (tVodadPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tVodadPage, "操作成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, PARAMETER_ERROR);
            }
        } catch (Exception e) {
            logger.error("[getVodadList][error]", e);
            jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, INTERFACE_ERROR);
        }
        return jsonStrResp;
    }

    public String delVodad(String id) {
        try {
            TFilter tFilter = new TFilter();
            VodWebService.Iface service = ServiceUtil.getVodWebService();
            if (StringUtils.isBlank(id)) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, PARAMETER_ERROR);
            }
            TResponse response = service.delAd(Long.parseLong(id));

            if (response == null) {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, INTERFACE_ERROR);
            } else if (Constants.CODE_SUC.equals(response.getCode())) {
                return Constants.getResponse(Constants.CODE_SUC, null, "删除成功").toJsonString();
            } else {
                return Constants.getJsonResponse(Constants.CODE_FAIL, null, "删除失败");
            }
        } catch (Exception e) {
            logger.error("[delVodad]", e);
            return Constants.getJsonResponse(Constants.CODE_ERROR, null, ERROR_EXIST);
        }
    }
}
